<template>
  <CommonPage>
    <a-row type="flex" class="flex-1 overflow-hidden">
      <a-col flex="300px" class="h-full flex-col overflow-hidden">
        <dict-list v-model:currentDict="currentId"></dict-list>
      </a-col>
      <a-col flex="1">
        <AppCrud ref="tableRef" :columns="columns" :get-data="renderDetail">
          <query-item label="名称">
            <a-input size="small" placeholder="请输入名称" allow-clear></a-input>
          </query-item>
          <template #action>
            <a-button type="primary" @click="formEl?.onOpen('add')">
              <template #icon>
                <PlusOutlined></PlusOutlined>
              </template>
              新增字典项
            </a-button>
          </template>
        </AppCrud>
      </a-col>
    </a-row>
  </CommonPage>
  <dict-form ref="formEl" type="字典项" @add="onAdd" @update="onUpdate"></dict-form>
</template>

<script setup lang="ts">
import { AppCrud, CommonPage, QueryItem } from '@/components/common'
import { Button, message, Modal, Space } from 'ant-design-vue'
import DictList from './components/dict-list.vue'
import { ref } from 'vue'
import dictApi, { DictDto, dictItemApi } from '@/api/dict'
import { PlusOutlined } from '@ant-design/icons-vue'
import DictForm from '@/views/system/dict/components/dict-form.vue'

const currentId = ref<string | null>(null)
const loading = ref(false)
const tableRef = ref()
const dictItemList = ref<any[]>([])
const formEl = ref()
const columns = [
  {
    title: '字典项名称',
    dataIndex: 'name',
  },
  {
    title: '字典项编码',
    dataIndex: 'code',
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
  {
    title: '排序',
    dataIndex: 'weight',
  },
  {
    title: '操作',
    align: 'center',
    width: 140,
    render: ({ record }: any) => {
      return h(
        Space,
        {},
        {
          default: () => [
            h(
              Button,
              {
                size: 'small',
                type: 'primary',
                onClick: () => formEl.value?.onOpen('edit', record),
              },
              {
                default: () => '编辑',
              },
            ),
            h(
              Button,
              {
                size: 'small',
                danger: true,
                onClick: () => handleDelete(record),
              },
              {
                default: () => '删除',
              },
            ),
          ],
        },
      )
    },
  },
]

watch(currentId, () => {
  tableRef.value?.handleSearch()
})

function handleDelete(record: any) {
  Modal.confirm({
    title: '提示',
    type: 'info',
    content: `确认删除【${record.name}】?`,
    onOk() {
      dictItemApi(currentId.value!)
        .delete(record.id!)
        .then(() => {
          message.success('删除成功')
          tableRef.value?.handleSearch()
        })
    },
    onCancel() {},
  })
}

const onAdd = (form: DictDto, callback: Function) => {
  dictItemApi(currentId.value!)
    .create(form)
    .then(() => {
      tableRef.value?.handleSearch()
      callback()
    })
}
const onUpdate = (form: any, callback: Function) => {
  dictItemApi(currentId.value!)
    .update(form.id, form)
    .then(() => {
      tableRef.value?.handleSearch()
      callback()
    })
}

async function renderDetail() {
  if (currentId.value) {
    loading.value = true
    return await dictApi.get(currentId.value).then(({ data }: any) => {
      dictItemList.value = data.items
      loading.value = false
      return {
        data: {
          record: data.items,
          total: data.items.length,
          page: 1,
          size: 10,
        },
      }
    })
  }
}
</script>

<style scoped></style>
